#include<Math.h>
using namespace std;
class Solution {
public:
    int getnextnumber(int x)
    {
        int ret = 0;
        while (x)
        {
            ret += pow(x % 10, 2);
            x = x / 10;
        }
        return ret;
    }
    bool isHappy(int n) {
        int slow = n, quick = n;
        while (1)
        {
            slow = getnextnumber(slow);
            quick = getnextnumber(getnextnumber(quick));
            if (slow == quick)
                break;
        }
        return slow == 1;
    }
};